﻿using System;
using System.Windows.Forms;
using XCI.Helper;
using XCI.Sys.Service;
using XCI.Windows.Helper;

namespace XCI.Sys.Forms
{
    public partial class FrmModuleDetails : FrmSysBase
    {
        private string id;

        public FrmModuleDetails(string p_id)
        {
            InitializeComponent();
            id = p_id;
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keys)
        {
            return OnEsc(msg, keys, DetailsFormEscResultCancel);
        }

        /// <summary>
        /// 页面初始化
        /// </summary>
        protected void InitForm()
        {
            BindModel();
        }

        /// <summary>
        /// 绑定数据到界面
        /// </summary>
        private void BindModel()
        {
            var model = ModuleService.Instance.SelectById(id);
            if (model == null) return;

            WinHelper.SetFormData(layoutPanel, model);

            //上级信息
            if (!string.IsNullOrEmpty(model.ParentId))
            {
                var pmodel = ModuleService.Instance.SelectById(model.ParentId);
                if (pmodel != null)
                {
                    editParentName.Text = pmodel.Name;
                }
            }

            //win图标
            editWinIcon.Text = $"大:({model.WinLargeIcon}) 小:({model.WinSmallIcon})";

            //获取最后一次操作人信息
            var hmodel = HistoryService.Instance.SelectLastByPrimaryKey(id);
            if (hmodel != null)
            {
                editOperateUserName.Text = hmodel.OperateUserName;
                editOperateDateTime.Text = DateTimeHelper.FormatDateHasSecond(hmodel.OperateDateTime);
            }

            recordHistoryGridControl1.PrimaryKey = id;
        }


        private void FrmModuleDetailsLoad(object sender, EventArgs e)
        {
            InitForm();
        }

        private void BtnClose_Click(object sender, EventArgs e)
        {
            ResultCancel();
        }
    }
}